﻿using System;
using System.Collections.Generic;
using System.Text;
using Core.Domain;

namespace Core.SyntaxAnalysis.Classes
{
    public class Declaration
    {
        public readonly DataType DeclarationType;
        public readonly List<Identificator> Identificators;

        public Declaration(List<Identificator> variables, DataType type)
        {
            Identificators = variables;
            DeclarationType = type;
        }

        public bool SemanticCheck(ref List<CompilerError> errors)
        {
            if (Identificators != null)
            {
                foreach (var id in Identificators)
                {
                    try
                    {
                        id.BindType(DeclarationType);
                    }
                    catch (ArgumentException)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public void Generate(StringBuilder s)
        {
            foreach (var id in Identificators)
            {
                s.AppendLine(DeclarationType + " " + id.Name);
            }
        }
    }
}